package com.giscloud.limp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.StringUtils;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.*;
import com.giscloud.limp.entity.*;
import com.giscloud.limp.entity.dto.CaseFirstInstanceDTO;
import com.giscloud.limp.entity.vo.CaseInfoVO;
import com.giscloud.limp.service.CaseFirstInstanceService;
import com.giscloud.limp.service.CaseWarnService;
import com.giscloud.redis.utils.RedisUtils;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;


@Service("caseFirstInstanceService")
@Log4j2
public class CaseFirstInstanceServiceImpl implements CaseFirstInstanceService {

    //一审
    @Autowired
    private final  CaseFirstInstanceDao caseFirstInstanceDao;

    //二审
    @Autowired
    private CaseSecondInstanceDao caseSecondInstanceDao;

    //再审
    @Autowired
    private CaseRetrialDao caseRetrialDao;

    //执行
    @Autowired
    private CaseExecutionDao caseExecutionDao;

    //结案
    @Autowired
    private CaseCloseDao caseCloseDao;

    //项目
    @Autowired
    private LegalProjectDao legalProjectDao;

    //被告
    @Autowired
    private DefendantDao defendantDao;

    //原告
    @Autowired
    private PlaintiffDao plaintiffDao;

    @Autowired
    private TokenUserUtil tokenUserUtil;

    @Autowired
    private CaseWarnService caseWarnService;

    @Autowired
    private CaseWarnDao caseWarnDao;


    @Autowired
    public CaseFirstInstanceServiceImpl(CaseFirstInstanceDao caseFirstInstanceDao) {
        this.caseFirstInstanceDao = caseFirstInstanceDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseFirstInstanceDTO caseFirstInstanceDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseFirstInstanceDTO));
        PageHelper.startPage(caseFirstInstanceDTO.getPage(), caseFirstInstanceDTO.getRows());
        if(StringUtils.isNotBlank(caseFirstInstanceDTO.getLitigationAmountStr())){
            String[] strs = caseFirstInstanceDTO.getLitigationAmountStr().split(",");
            if(StringUtils.isNotBlank(strs[0])){
                caseFirstInstanceDTO.setLitigationAmountMin(new BigDecimal(strs[0]));
            }
            if(strs.length > 1 && StringUtils.isNotBlank(strs[1])){
                caseFirstInstanceDTO.setLitigationAmountMax(new BigDecimal(strs[1]));
            }
        }
        List<CaseFirstInstance> caseFirstInstances = caseFirstInstanceDao.selectByExample(caseFirstInstanceDTO, tokenUserUtil.getOrgId());
        return PageInfo.of(caseFirstInstances);
    }

    /**
     *
     * 导出
     */
    @Override
    public List<CaseFirstInstance> exportList(CaseFirstInstanceDTO caseFirstInstanceDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseFirstInstanceDTO));
        if(caseFirstInstanceDTO == null){
            caseFirstInstanceDTO = new CaseFirstInstanceDTO();
        }
        List<CaseFirstInstance> caseFirstInstances = caseFirstInstanceDao.selectByExample(caseFirstInstanceDTO,tokenUserUtil.getOrgId());
        return caseFirstInstances;
    }


    @Override
    public Result<CaseInfoVO> caseInfo(CaseFirstInstanceDTO caseFirstInstanceDTO) {
        if(caseFirstInstanceDTO.getCaseId() == null){
            return Result.error(-1,"caseId 必填");
        }
        CaseInfoVO caseInfoVO = new CaseInfoVO();

        //查询一审信息
        CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectByPrimaryKey(caseFirstInstanceDTO.getCaseId());
        if(caseFirstInstance == null){
            return Result.error(-1,"此 caseId 无数据");
        }
        caseFirstInstance.setPlaintiffs(new ArrayList<>());
        caseFirstInstance.setDefendants(new ArrayList<>());
        caseInfoVO.setCaseFirstInstance(caseFirstInstance);

        //组装展示用的案件说明
        StringBuilder caseDesc = new StringBuilder();
        caseDesc.append(caseFirstInstance.getCaseName()+"一案(");
        caseDesc.append(caseFirstInstance.getFirstCaseCode()+"),目前正处于");
        caseDesc.append(getDictName("litigation_stage",caseFirstInstance.getLitigationStage()) + "阶段，涉案金额");
        caseDesc.append(caseFirstInstance.getLitigationAmount()+"元");
        String caseHandlingOpinions = caseFirstInstance.getCaseHandlingOpinions();

        //查询二审
        CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
        caseSecondInstanceExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseSecondInstance> caseSecondInstances = caseSecondInstanceDao.selectByExample(caseSecondInstanceExample);
        CaseSecondInstance caseSecondInstance = null;
        if(CollectionUtils.isEmpty(caseSecondInstances)){
            caseSecondInstance = new CaseSecondInstance();
        }else{
            caseSecondInstance = caseSecondInstances.get(0);
            caseHandlingOpinions = caseSecondInstance.getCaseHandlingOpinions();
        }
//        caseSecondInstance.setPlaintiffs(new ArrayList<>());
//        caseSecondInstance.setDefendants(new ArrayList<>());
        caseInfoVO.setCaseSecondInstance(caseSecondInstance);

        //查询再审
        CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
        caseRetrialExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseRetrial> caseRetrials = caseRetrialDao.selectByExample(caseRetrialExample);
        CaseRetrial caseRetrial = null;
        if(CollectionUtils.isEmpty(caseRetrials)){
            caseRetrial = new CaseRetrial();
        }else{
            caseRetrial = caseRetrials.get(0);
            caseHandlingOpinions = caseRetrial.getCaseHandlingOpinions();
        }
//        caseRetrial.setPlaintiffs(new ArrayList<>());
//        caseRetrial.setDefendants(new ArrayList<>());
        caseInfoVO.setCaseRetrial(caseRetrial);


        //查询执行
        CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
        caseExecutionExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseExecution> caseExecutions = caseExecutionDao.selectByExample(caseExecutionExample);
        CaseExecution caseExecution = null;
        if(CollectionUtils.isEmpty(caseExecutions)){
            caseExecution = new CaseExecution();
        }else{
            caseExecution = caseExecutions.get(0);
            caseHandlingOpinions = caseExecution.getCaseHandlingOpinions();
        }
        caseInfoVO.setCaseExecution(caseExecution);

        //查询结案
        CaseCloseExample caseCloseExample = new CaseCloseExample();
        caseCloseExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseClose> caseCloses = caseCloseDao.selectByExample(caseCloseExample);
        CaseClose caseClose = null;
        if(CollectionUtils.isEmpty(caseCloses)){
            caseClose = new CaseClose();
        }else{
            caseClose = caseCloses.get(0);
            caseHandlingOpinions = caseClose.getClosecaseDescription();
        }
        caseInfoVO.setCaseClose(caseClose);

        if(StringUtils.isNotBlank(caseHandlingOpinions)){
            caseDesc.append(","+caseHandlingOpinions);
        }
        //设置结案说明
        caseFirstInstance.setCaseDescription(caseDesc.toString());

        //查询案件原告，按案件阶段组装进对于案件信息中
        PlaintiffExample plaintiffExample = new PlaintiffExample();
        plaintiffExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<Plaintiff> plaintiffs = plaintiffDao.selectByExample(plaintiffExample);
        if(CollectionUtils.isNotEmpty(plaintiffs)){
            for(Plaintiff plaintiff:plaintiffs){
                if(plaintiff.getLitigationStage() == 1){
                    caseFirstInstance.getPlaintiffs().add(plaintiff);
                }else if(plaintiff.getLitigationStage() == 2){
                    if(caseSecondInstance.getPlaintiffs() == null){
                        caseSecondInstance.setPlaintiffs(new ArrayList<>());
                    }
                    caseSecondInstance.getPlaintiffs().add(plaintiff);
                }else if(plaintiff.getLitigationStage() == 3){
                    if(caseRetrial.getPlaintiffs() == null){
                        caseRetrial.setPlaintiffs(new ArrayList<>());
                    }
                    caseRetrial.getPlaintiffs().add(plaintiff);
                }
            }
        }
        //查询案件被告，按案件阶段组装进对于案件信息中
        DefendantExample defendantExample = new DefendantExample();
        defendantExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<Defendant> defendants = defendantDao.selectByExample(defendantExample);
        if(CollectionUtils.isNotEmpty(defendants)){
            for(Defendant defendant:defendants){
                if(defendant.getLitigationStage() == 1){
                    caseFirstInstance.getDefendants().add(defendant);
                }else if(defendant.getLitigationStage() == 2){
                    if(caseSecondInstance.getDefendants() == null){
                        caseSecondInstance.setDefendants(new ArrayList<>());
                    }
                    caseSecondInstance.getDefendants().add(defendant);
                }else if(defendant.getLitigationStage() == 3){
                    if(caseRetrial.getDefendants() == null){
                        caseRetrial.setDefendants(new ArrayList<>());
                    }
                    caseRetrial.getDefendants().add(defendant);
                }
            }
        }
        CaseWarnExample caseWarnExample = new CaseWarnExample();
        caseWarnExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseWarn> caseWarns = caseWarnDao.selectByExample(caseWarnExample);
        if(CollectionUtils.isNotEmpty(caseWarns)){
            for(CaseWarn caseWarn:caseWarns){
                if(caseWarn.getLitigationStage() == 1){
                    caseFirstInstance.setWarnContent(caseWarn.getWarnContent());
                    caseFirstInstance.setWarnDate(caseWarn.getWarnDate());
                }else if(caseWarn.getLitigationStage() == 2){
                    caseSecondInstance.setWarnContent(caseWarn.getWarnContent());
                    caseSecondInstance.setWarnDate(caseWarn.getWarnDate());
                }else if(caseWarn.getLitigationStage() == 3){
                    caseRetrial.setWarnContent(caseWarn.getWarnContent());
                    caseRetrial.setWarnDate(caseWarn.getWarnDate());
                } else if(caseWarn.getLitigationStage() == 4){
                    caseExecution.setWarnContent(caseWarn.getWarnContent());
                    caseExecution.setWarnDate(caseWarn.getWarnDate());
                } else if(caseWarn.getLitigationStage() == 5){
                    caseClose.setWarnContent(caseWarn.getWarnContent());
                    caseClose.setWarnDate(caseWarn.getWarnDate());
                }
            }
        }
        return Result.success(caseInfoVO);
    }


    /**
     * 信息
     */
    @Override
    public CaseFirstInstance getById(Integer caseId){
        log.debug("#getById parameter ={}", caseId);
        return caseFirstInstanceDao.selectByPrimaryKey(caseId);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(CaseFirstInstance caseFirstInstance){
        log.debug("#save parameter ={}", JSON.toJSONString(caseFirstInstance));
        if(caseFirstInstance.getCaseType() == null){
            return Result.error("未录入案件类别");
        }
        if(StringUtils.isBlank(caseFirstInstance.getFirstCaseCode())){
            return Result.error("未录入一审案件号");
        }
        if(caseFirstInstance.getActionCause() == null){
            return Result.error("未录入案由");
        }
        if(CollectionUtils.isEmpty(caseFirstInstance.getPlaintiffs()) || CollectionUtils.isEmpty(caseFirstInstance.getDefendants())){
            return Result.error("未录入原告或被告信息");
        }
        caseFirstInstance.setActionCauseStr(getDictName("action_cause",caseFirstInstance.getActionCause()));

        StringBuilder plaintiffStr = new StringBuilder();
        StringBuilder defendantStr = new StringBuilder();
        for(Plaintiff plaintiff : caseFirstInstance.getPlaintiffs()){
            plaintiffStr.append(plaintiff.getPlaintiffName()+"、");
        }
        for(Defendant defendant : caseFirstInstance.getDefendants()){
            defendantStr.append(defendant.getDefendantName()+"、");
        }
        //组装案件名称  (原告)诉(被告)(案由)
        String caseName = plaintiffStr.substring(0,plaintiffStr.length()-1) + "诉" + defendantStr.substring(0,defendantStr.length()-1)+caseFirstInstance.getActionCauseStr();
        caseFirstInstance.setCaseName(caseName);

        String orgid = String.valueOf(tokenUserUtil.getOrgId());
        caseFirstInstance.setCreatedOrgId(Integer.valueOf(orgid));
        String orgCode = tokenUserUtil.getOrgCode();
        //案件编号 单位编号(org_id)+YYYYMMDD+案件类别(01/02)+编号(两位)
        if(orgCode.length() < 6){
            while(orgCode.length()<6){
                orgCode = "0" + orgCode;
            }
        }
        String dateStr = DateUtil.format(caseFirstInstance.getProsecutionDate(),"yyyyMMdd");
        String caseType = "0"+ (caseFirstInstance.getCaseType());
        String caseCode = orgCode + dateStr+caseType;
        CaseFirstInstanceExample example = new CaseFirstInstanceExample();
        example.createCriteria().andCaseCodeLike(caseCode+"%");
        Long count = caseFirstInstanceDao.countByExample(example);
        if((count + 1)<10){
            caseCode = caseCode + "0" + (count+1);
        }else{
            caseCode = caseCode + count;
        }
        caseFirstInstance.setCaseCode(caseCode);
        caseFirstInstance.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        caseFirstInstance.setLitigationStage(1);
        caseFirstInstance.setCaseStatus(0);
        caseFirstInstanceDao.insertSelective(caseFirstInstance);
        for(Plaintiff plaintiff : caseFirstInstance.getPlaintiffs()){
            plaintiff.setCaseId(caseFirstInstance.getCaseId());
            plaintiff.setLitigationStage(1);
            plaintiffDao.insertSelective(plaintiff);
        }
        for(Defendant defendant : caseFirstInstance.getDefendants()){
            defendant.setCaseId(caseFirstInstance.getCaseId());
            defendant.setLitigationStage(1);
            defendantDao.insertSelective(defendant);
        }
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseFirstInstance.getCaseId());
        caseWarn.setLitigationStage(1);
        caseWarn.setWarnDate(caseFirstInstance.getWarnDate());
        caseWarn.setWarnContent(caseFirstInstance.getWarnContent());
        caseWarnService.save(caseWarn);
        return Result.success(caseFirstInstance,"保存成功");
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(CaseFirstInstance caseFirstInstance){
         log.debug("#updateById parameter ={}", JSON.toJSONString(caseFirstInstance));
        if (caseFirstInstance.getCaseId() == null){
            return Result.error("caseId 必填");
        }
        if(caseFirstInstance.getCaseType() == null){
            return Result.error("未录入案件类别");
        }
        if(StringUtils.isBlank(caseFirstInstance.getFirstCaseCode())){
            return Result.error("未录入一审案件号");
        }
        if(caseFirstInstance.getActionCause() == null){
            return Result.error("未录入案由");
        }
        if(CollectionUtils.isEmpty(caseFirstInstance.getPlaintiffs()) || CollectionUtils.isEmpty(caseFirstInstance.getDefendants())){
            return Result.error("未录入原告或被告信息");
        }
        caseFirstInstance.setActionCauseStr(getDictName("action_cause",caseFirstInstance.getActionCause()));

        StringBuilder plaintiffStr = new StringBuilder();
        StringBuilder defendantStr = new StringBuilder();

        if(CollectionUtils.isNotEmpty(caseFirstInstance.getPlaintiffs())){
            PlaintiffExample plaintiffExample = new PlaintiffExample();
            plaintiffExample.createCriteria().andCaseIdEqualTo(caseFirstInstance.getCaseId()).andLitigationStageEqualTo(1);
            plaintiffDao.deleteByExample(plaintiffExample);
            for(Plaintiff plaintiff : caseFirstInstance.getPlaintiffs()){
                plaintiffStr.append(plaintiff.getPlaintiffName()+"、");
                plaintiff.setLitigationStage(1);
                plaintiff.setCaseId(caseFirstInstance.getCaseId());
                plaintiffDao.insertSelective(plaintiff);
            }
        }
        if(CollectionUtils.isNotEmpty(caseFirstInstance.getDefendants())){
            DefendantExample defendantExample = new DefendantExample();
            defendantExample.createCriteria().andCaseIdEqualTo(caseFirstInstance.getCaseId()).andLitigationStageEqualTo(1);
            defendantDao.deleteByExample(defendantExample);
            for(Defendant defendant : caseFirstInstance.getDefendants()){
                defendantStr.append(defendant.getDefendantName()+"、");
                defendant.setLitigationStage(1);
                defendant.setCaseId(caseFirstInstance.getCaseId());
                defendantDao.insertSelective(defendant);
            }
        }
        //组装案件名称  (原告)诉(被告)(案由)
        String caseName = plaintiffStr.substring(0,plaintiffStr.length()-1) + "诉" + defendantStr.substring(0,defendantStr.length()-1)+caseFirstInstance.getActionCauseStr();
        caseFirstInstance.setCaseName(caseName);
        CaseFirstInstanceExample caseFirstInstanceExample = new CaseFirstInstanceExample();
        CaseFirstInstanceExample.Criteria criteria = caseFirstInstanceExample.createCriteria();
        criteria.andCaseIdEqualTo(caseFirstInstance.getCaseId());
        caseFirstInstance.setOperateStaffId(tokenUserUtil.getUserId().intValue());
        caseFirstInstance.setVersion(new Date());
        caseFirstInstanceDao.updateByExampleSelective(caseFirstInstance,caseFirstInstanceExample);
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseFirstInstance.getCaseId());
        caseWarn.setLitigationStage(1);
        caseWarn.setWarnDate(caseFirstInstance.getWarnDate());
        caseWarn.setWarnContent(caseFirstInstance.getWarnContent());
        caseWarnService.updateById(caseWarn);
        return Result.success("修改成功");

    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> caseIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(caseIds));
        CaseFirstInstanceExample caseFirstInstanceExample = new CaseFirstInstanceExample();
        caseFirstInstanceExample.createCriteria().andCaseIdIn(caseIds);
        caseFirstInstanceDao.deleteByExample(caseFirstInstanceExample);

        CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
        caseFirstInstanceExample.createCriteria().andCaseIdIn(caseIds);
        caseSecondInstanceDao.deleteByExample(caseSecondInstanceExample);

        CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
        caseRetrialExample.createCriteria().andCaseIdIn(caseIds);
        caseRetrialDao.deleteByExample(caseRetrialExample);

        CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
        caseExecutionExample.createCriteria().andCaseIdIn(caseIds);
        caseExecutionDao.deleteByExample(caseExecutionExample);

        CaseCloseExample caseCloseExample = new CaseCloseExample();
        caseCloseExample.createCriteria().andCaseIdIn(caseIds);
        caseCloseDao.deleteByExample(caseCloseExample);

        PlaintiffExample plaintiffExample = new PlaintiffExample();
        plaintiffExample.createCriteria().andCaseIdIn(caseIds);
        plaintiffDao.deleteByExample(plaintiffExample);

        DefendantExample defendantExample = new DefendantExample();
        defendantExample.createCriteria().andCaseIdIn(caseIds);
        defendantDao.deleteByExample(defendantExample);

        for(Integer caseId : caseIds){
            CaseWarn caseWarn = new CaseWarn();
            caseWarn.setCaseId(caseId);
            caseWarn.setLitigationStage(1);
            caseWarnService.remove(caseWarn);
        }

        return Result.success("删除成功");
    }

    @Override
    public Map<String, String> downloadCaseDesc(CaseFirstInstanceDTO caseFirstInstanceDTO) {
        Map<String,String> map = new HashMap<>();
        //查询一审信息
        CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectByPrimaryKey(caseFirstInstanceDTO.getCaseId());
        if(caseFirstInstance == null){
            return null;
        }

        //组装展示用的案件说明
        StringBuilder caseDesc = new StringBuilder();
        caseDesc.append(caseFirstInstance.getCaseName()+"一案(");
        caseDesc.append(caseFirstInstance.getFirstCaseCode()+"),目前正处于");
        caseDesc.append(getDictName("litigation_stage",caseFirstInstance.getLitigationStage()) + "阶段，涉案金额");
        caseDesc.append(caseFirstInstance.getLitigationAmount()+"元");
        String caseHandlingOpinions = caseFirstInstance.getCaseHandlingOpinions();

        //查询二审
        CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
        caseSecondInstanceExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseSecondInstance> caseSecondInstances = caseSecondInstanceDao.selectByExample(caseSecondInstanceExample);
        CaseSecondInstance caseSecondInstance = null;
        if(CollectionUtils.isNotEmpty(caseSecondInstances)){
            caseSecondInstance = caseSecondInstances.get(0);
            caseHandlingOpinions = caseSecondInstance.getCaseHandlingOpinions();
        }

        //查询再审
        CaseRetrialExample caseRetrialExample = new CaseRetrialExample();
        caseRetrialExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseRetrial> caseRetrials = caseRetrialDao.selectByExample(caseRetrialExample);
        CaseRetrial caseRetrial = null;
        if(CollectionUtils.isNotEmpty(caseRetrials)){
            caseRetrial = caseRetrials.get(0);
            caseHandlingOpinions = caseRetrial.getCaseHandlingOpinions();
        }

        //查询执行
        CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
        caseExecutionExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseExecution> caseExecutions = caseExecutionDao.selectByExample(caseExecutionExample);
        CaseExecution caseExecution = null;
        if(CollectionUtils.isNotEmpty(caseExecutions)){
            caseExecution = caseExecutions.get(0);
            caseHandlingOpinions = caseExecution.getCaseHandlingOpinions();
        }

        //查询结案
        CaseCloseExample caseCloseExample = new CaseCloseExample();
        caseCloseExample.createCriteria().andCaseIdEqualTo(caseFirstInstanceDTO.getCaseId()).andStatusCdEqualTo(1);
        List<CaseClose> caseCloses = caseCloseDao.selectByExample(caseCloseExample);
        CaseClose caseClose = null;
        if(CollectionUtils.isNotEmpty(caseCloses)){
            caseClose = caseCloses.get(0);
            caseHandlingOpinions = caseClose.getClosecaseDescription();
        }

        if(StringUtils.isNotBlank(caseHandlingOpinions)){
            caseDesc.append(","+caseHandlingOpinions);
        }
        //设置结案说明
        caseFirstInstance.setCaseDescription(caseDesc.toString());

        map.put("caseName",caseFirstInstance.getCaseName());
        map.put("caseDesc",caseFirstInstance.getCaseDescription());
        return map;
    }

    @Override
    public Result litigationAmountByYear(Map<String, Object> params) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>> pubDictValues = caseFirstInstanceDao.queryDictValue("litigation_amount",null);
        if(CollectionUtils.isNotEmpty(pubDictValues)){
            for(Map<String,Object> map : pubDictValues){
                String name = String.valueOf(map.get("name"));
                String remark = String.valueOf(map.get("remark"));
                String[] strs = remark.split(",");
                String litigationAmountSql = "";
                if(StringUtils.isNotBlank(strs[0])){
                    litigationAmountSql = "litigation_amount >= " + strs[0];
                }
                if(strs.length > 1 && StringUtils.isNotBlank(strs[1])){
                    if(!"".equals(litigationAmountSql)){
                        litigationAmountSql += " and ";
                    }
                    litigationAmountSql += "litigation_amount < " + strs[1];
                }
                params.put("litigationAmountSql",litigationAmountSql);
                Map<String,Object> queryResult = caseFirstInstanceDao.litigationAmountByYear(params);
                queryResult.put("name",name);
                result.add(queryResult);
            }
        }
        return Result.success(result);
    }

    @Override
    public Result litigationTotalAmountByYear(Map<String, Object> params) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>> pubDictValues = caseFirstInstanceDao.queryDictValue("litigation_amount",null);
        if(CollectionUtils.isNotEmpty(pubDictValues)){
            for(Map<String,Object> map : pubDictValues){
                String name = String.valueOf(map.get("name"));
                String remark = String.valueOf(map.get("remark"));
                String[] strs = remark.split(",");
                String litigationAmountSql = "";
                if(StringUtils.isNotBlank(strs[0])){
                    litigationAmountSql = "litigation_amount >= " + strs[0];
                }
                if(strs.length > 1 && StringUtils.isNotBlank(strs[1])){
                    if(!"".equals(litigationAmountSql)){
                        litigationAmountSql += " and ";
                    }
                    litigationAmountSql += "litigation_amount < " + strs[1];
                }
                params.put("litigationAmountSql",litigationAmountSql);
                Map<String,Object> queryResult = caseFirstInstanceDao.litigationTotalAmountByYear(params);
                queryResult.put("name",name);
                result.add(queryResult);
            }
        }
        return Result.success(result);
    }

    /**
     * 获取字典值
     * @param dictKey
     * @param code
     * @return
     */
    private String getDictName(String dictKey, Integer code){
        List<Map<String,Object>> pubDictValues = caseFirstInstanceDao.queryDictValue(dictKey,code);
        if(CollectionUtils.isNotEmpty(pubDictValues)){
            return String.valueOf(pubDictValues.get(0).get("name"));
        }else{
            return "";
        }
    }

}